package com.zeng.jiudian.servlet.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.bean.ProductModel;
import com.zeng.jiudian.service.ProductService;
import com.zeng.jiudian.service.impl.ProductServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/23 - 17:19
 */
@WebServlet("/back/product/findAll")
public class ProductListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
        //处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String productName = req.getParameter("productName");
        String enName = req.getParameter("enName");
        String marketPrice = req.getParameter("marketPrice");
        String price = req.getParameter("price");
        String content = req.getParameter("content");
        String introduction = req.getParameter("introduction");
        String productTypeId = req.getParameter("productTypeId");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String nowNum = req.getParameter("nowNum");

        ProductModel productModel = new ProductModel();
        productModel.setProductName(productName);
        productModel.setEnName(enName);
        productModel.setMarketPrice((marketPrice == null || "".equals(marketPrice)) ? -1 : Double.parseDouble(marketPrice));
        productModel.setPrice((price == null || "".equals(price)) ? -1 : Double.parseDouble(price));
        productModel.setContent(content);
        productModel.setProductTypeId((productTypeId == null || "".equals(productTypeId)) ? 0 : Integer.parseInt(productTypeId));
        productModel.setOrder(order);
        productModel.setLift(lift);
        productModel.setIntroduction(introduction);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));
        productModel.setPageModel(pageModel);

        ProductService productService = new ProductServiceImpl();
        List<ProductModel> list = productService.findAll(productModel);
        int count = productService.getCount(productModel);

        String listString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray listArray = JSONArray.parseArray(listString);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", listArray);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
